package apiTest

import (
	"api_server/hadnler/dao"
	"api_server/hadnler/model"
	"api_server/hadnler/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Test(c *gin.Context) {
	var req request.TestExam
	var t model.ExamTest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "验证失败",
		})
		return
	}
	t = model.ExamTest{
		Id:             req.Id,
		Administrators: req.Administrators,
	}
	if !dao.GetOneByFields(&model.ExamTest{}, &t) {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusBadRequest,
			"msg":  nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
	})
}

func Regitster(c *gin.Context) {
	var req request.Register
	var user model.User
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "验证失败",
		})
		return
	}
	user = model.User{
		Account:  req.Account,
		PassWord: req.PassWord,
	}
	if user.Account == req.Account {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "此用户已存在",
		})
		return
	}
	if !dao.Create(&user) {
		c.JSON(http.StatusOK, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "注册失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
	})
}
